package com.gizwits.akka

import akka.actor.{ActorRef, Props, ActorSystem, Actor}


case class PING(msg: String)

case class Person(name: String)


case class InitMsg(msg: String)

class Myactor extends Actor {
  var pname: String = _

  override def receive: Receive = {
    case InitMsg(name) => this.pname = name

    case Person(who) => {
      println("who  ack ---" + this.toString + "----" + who)
      context.actorSelection(who).tell(new PING(pname), self)


    }
    case PING(msg) => {
      println("from----" + msg)
      Thread.sleep(1000)
      // sender().tell(new PING(pname), self)
      sender ! new PING(pname)

    }
    case _ => println("---haha---")


  }
}

object PingPangActor {


  def main(args: Array[String]) {


    val system = ActorSystem("HelloSystem")
    val p1 = system.actorOf(Props[Myactor], name = "p1")
    val p2 = system.actorOf(Props[Myactor], name = "p2")
    // p1.tell(new InitMsg("p1"), ActorRef.noSender)
    //p2.tell(new InitMsg("qingqing"), ActorRef.noSender)

    //who  start
//    p1.tell(new Person(p2.path.toSerializationFormat), ActorRef.noSender)
//    p2.tell(new Person(p1.path.toSerializationFormat), ActorRef.noSender)
    //init person name

    p1 ! new InitMsg("p1")
    p2 ! new InitMsg("qingqing")

    p1 ! new Person(p2.path.toSerializationFormat)

    // p1 ! new Person(p2.path.toSerializationFormat)


  }
}
